﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.XtraPivotGrid;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using XCI.Extensions;
using XCI.Helper;
using XCI.Windows;

namespace XCI.Setting.Controls
{
    public partial class SettingPivotGridFields : BaseUserControl
    {
        private PivotGridControlPlus pivotGrid;

        public SettingPivotGridFields()
        {
            InitializeComponent();
        }

        public void Init(PivotGridControlPlus pivotGrid)
        {
            this.pivotGrid = pivotGrid;
            if (pivotGrid == null) return;
            LoadData();
            SetStatus();
        }

        protected void LoadData()
        {
            listBoxControl1.Items.BeginUpdate();
            listBoxControl1.Items.Clear();

            for (var index = 0; index < pivotGrid.Fields.Count; index++)
            {
                var item = pivotGrid.Fields[index];
                var name = $"字段{index + 1}";
                if (!string.IsNullOrWhiteSpace(item.FieldName))
                {
                    name = $"{item.FieldName}";
                    if (!string.IsNullOrWhiteSpace(item.Caption))
                    {
                        name += $"-{item.Caption}";
                    }
                }
                listBoxControl1.Items.Add(name);
            }

            listBoxControl1.Items.EndUpdate();
        }

        protected List<PivotGridField> ToFieldList()
        {
            var list = new List<PivotGridField>(pivotGrid.Fields.Count);
            foreach (PivotGridField field in pivotGrid.Fields)
            {
                list.Add(field);
            }
            return list;
        }

        protected void ToFieldCollection(List<PivotGridField> list)
        {
            pivotGrid.Fields.Clear();
            list.ForEach(p => pivotGrid.Fields.Add(p));
        }

        private void SetStatus()
        {
            var index = listBoxControl1.SelectedIndex;
            btnDelete.Enabled = index > -1;
            btnUp.Enabled = index > 0;
            btnDown.Enabled = index >= 0 && index < pivotGrid.Fields.Count - 1;
        }

        private void MoveItem(int index, bool isUp)
        {
            var list = ToFieldList();
            if (isUp)
            {
                list.MoveUp(index);
            }
            else
            {
                list.MoveDown(index);
            }
            ToFieldCollection(list);
            LoadData();
            listBoxControl1.SelectedIndex = isUp ? (index - 1) : (index + 1);
        }

        private void listBoxControl1_SelectedValueChanged(object sender, EventArgs e)
        {
            SetStatus();
            var index = listBoxControl1.SelectedIndex;
            if (index == -1) return;

            propertyGridPlus.SelectedObject = pivotGrid.Fields[index];
        }

        private void xciPropertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.Label != null && (e.ChangedItem.Label.Equals("FieldName") || e.ChangedItem.Label.Equals("Caption")))
            {
                LoadData();
            }
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var fieldIndex = pivotGrid.Fields.Count;
            var item = new DevExpress.XtraPivotGrid.PivotGridField();
            item.Area = DevExpress.XtraPivotGrid.PivotArea.ColumnArea;
            item.AreaIndex = fieldIndex;
            item.Name = $"pivotGridField{fieldIndex}";
            pivotGrid.Fields.Add(item);
            LoadData();
            listBoxControl1.SelectedIndex = pivotGrid.Fields.Count - 1;
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中项吗？") == DialogResult.Yes)
            {
                pivotGrid.Fields.RemoveAt(listBoxControl1.SelectedIndex);
                LoadData();
            }
        }

        private void btnUp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MoveItem(listBoxControl1.SelectedIndex, true);
        }

        private void btnDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MoveItem(listBoxControl1.SelectedIndex, false);
        }

        private void btnAuto_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowMessageYesNo("当前字段将被清除,确定自动生成字段吗？") != DialogResult.Yes) return;
            pivotGrid.AutoBuildColumn();
            LoadData();
            listBoxControl1.SelectedIndex = 0;
        }

    }
}
